﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using BUS;
using DAO;
using DTO;
namespace baitaptuan5
{
    public partial class NhapSach : Form
    {
        public NhapSach()
        {
            InitializeComponent();
            pnbus = new PhieuNhapBUS();
            pndto = new PhieuNhapDTO();
            sachbus = new SachBUS();
            sachdto = new SachDTO();
            chitetbus = new ChiTietPhieuBUS();
            chitetdto = new ChiTietPhieuNhapDTO();
            dtSach = sachbus.laybang();
            dtPhieuNhap = pnbus.LayTable();
            dem();

        }
        PhieuNhapDTO pndto;
        PhieuNhapBUS pnbus;
        SachBUS sachbus;
        SachDTO sachdto;
        ChiTietPhieuNhapDTO chitetdto;
        ChiTietPhieuBUS chitetbus;
        DataTable dtPhieuNhap, dtChiTiet, dtSach;

        int idem, tien,tong;
        void dem()
        {
            idem = 0;
            dataGridView1.DataSource = dtPhieuNhap;
            foreach (DataRow row in dtPhieuNhap.Rows)
            {
                idem++;
            }
            toolStripStatusLabel2.Text = idem.ToString();
        }
        void TongTien()
        {

            dtChiTiet = chitetbus.LayTableTheoMaPhieu(pndto);
            dataGridView2.DataSource = dtChiTiet;
            tien = 0;
            tong = 0;
            foreach (DataRow row in dtChiTiet.Rows)
            {
                string filterExpression = "";

                filterExpression = "MaSach =" + row["MaSach"].ToString();

                DataRow[] rows = dtSach.Select(filterExpression);
                tien = int.Parse(rows[0].ItemArray[5].ToString()) * int.Parse(row["SoLuongNhap"].ToString());
                tong = tong + tien;
            }
            tongtien1.Text = "TỔNG TIỀN :"+ tong.ToString();

        }
        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void dataGridView1_SelectionChanged(object sender, EventArgs e)
        {
            try
            {
                int r = this.dataGridView1.CurrentCell.RowIndex;
                pndto.MaPN = int.Parse(dataGridView1.Rows[r].Cells[0].Value.ToString());
                pndto.NgayNhap = DateTime.Parse(dataGridView1.Rows[r].Cells[1].Value.ToString());
                TongTien();
            }
            catch (Exception)
            {
            }
        }

        private void dateTimePicker1_ValueChanged(object sender, EventArgs e)
        {
            dtPhieuNhap = pnbus.LayTableTheoNgay(dateTimePicker1.Value, dateTimePicker2.Value);
            dem();
        }

        private void dateTimePicker2_ValueChanged(object sender, EventArgs e)
        {
            dtPhieuNhap = pnbus.LayTableTheoNgay(dateTimePicker1.Value, dateTimePicker2.Value);
            dem();
        }

        private void toolStripStatusLabel2_Click(object sender, EventArgs e)
        {

        }

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            pndto = new PhieuNhapDTO();
            ThemPhieuNhap themphieu = new ThemPhieuNhap(pndto);
            themphieu.ShowDialog();
        }

        private void toolStripButton3_Click(object sender, EventArgs e)
        {
            ThemPhieuNhap themphieu = new ThemPhieuNhap(pndto);
            themphieu.ShowDialog();
        }

        private void toolStripButton4_Click(object sender, EventArgs e)
        {
            dtPhieuNhap = pnbus.LayTable();
            dem();
        }

        private void toolStripButton2_Click(object sender, EventArgs e)
        {
            try
            {
                DialogResult result = MessageBox.Show("CÓ THỰC SỰ MUỐN XÓA PHIẾU NHẬP NÀY KHÔNG", "THÔNG BÁO", MessageBoxButtons.YesNo);
                 if (result == DialogResult.Yes)
                 {
                     pnbus.Xoa(pndto);
                     MessageBox.Show("XÓA THÀNH CÔNG", "THÔNG BÁO");
                     dem();
                 }
            }
            catch(Exception)
            {
                MessageBox.Show("YÊU CẦU XÓA HẾT CHI TIẾT CỦA PHIẾU NHẬP", "THÔNG BÁO");
            }
            
        }

        private void panel1_Paint(object sender, PaintEventArgs e)
        {

        }

        private void splitContainer1_Panel2_Paint(object sender, PaintEventArgs e)
        {

        }
    }
}
